package com.wizardClient;

import barecodes.Ean18Generator;
import calculations.PassCost;
import com.appclient.DaoInstanceEnum;
import com.carpark.ejb.Model.AccountingdataModel;
import com.carpark.ejb.Model.CarsModel;
import com.carpark.ejb.Model.ClientsModel;
import com.carpark.ejb.Model.TarrifsModel;
import java.net.URL;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import printer.PrinterInstance;

/**
 *
 * @author Piotr
 */
public class EndPageController implements Initializable, ClientControlledScreen {
    private ClientScreenController myController;
    @FXML
    private Label clientName;
    @FXML
    private Label clientAdress;
    @FXML
    private Label plate;
    @FXML
    private Label carType;
    @FXML
    private Label payName;
    @FXML
    private Label payAdress;
    @FXML
    private Label space;
    @FXML
    private Label pass;
    @FXML
    private Label passTo;
    @FXML
    private Label cost;
    @FXML
    private ImageView barecode;
    private Image barImage;
    private void initialize() {
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        initialize();
    }

    @Override
    public void setScreenParent(ClientScreenController screenParent) {
        myController = screenParent;
    }
    
    public ClientScreenController getScreenParent() {
        return myController;
    }

    @Override
    public boolean validateForm() {
        return true;
    }

    public void loadData(ClientsModel client, Date startDate, Date endDate, CarsModel car, AccountingdataModel accountingData, int tarrifId, Integer choosedSpace) {
        this.clientName.setText(client.getLastName()+", "+client.getName());
        String adress = client.getZipCode()+ " "+client.getCity()+", "+client.getStreet()+" "+client.getHouseNumber();
        if(client.getLocalNumber() != null){
            adress+="/"+client.getLocalNumber().toString();
        }
        this.clientAdress.setText(adress);
        this.plate.setText("Nr rejestracyjny: "+car.getNumberPlate());
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss ");
        this.pass.setText("Ważna od: "+sdf.format(startDate));
        this.passTo.setText("do: "+sdf.format(endDate));
        this.carType.setText("Typ pojazdu: "+car.getCarTypeInPolish());
        String payerAdress = accountingData.getZipNumber()+" "+accountingData.getCity()+", "+accountingData.getStreet()+" "+accountingData.getHouseNumber();
        if(accountingData.getLocalNumber() != null){
            payerAdress+="/"+accountingData.getLocalNumber().toString();
        }
        this.payAdress.setText(payerAdress);
        this.payName.setText(accountingData.getName()+", nip: "+accountingData.getNipNumber());
        this.space.setText(choosedSpace.toString());
        PassCost passCost = new PassCost();
        TarrifsModel tarrif = DaoInstanceEnum.INSTANCE.getDao().getTarrifsById(tarrifId);
        DecimalFormat df = new DecimalFormat("#.##");
        this.cost.setText("Do zapłaty: "+df.format(passCost.countCostOfPass(startDate, endDate, tarrif))+" zł");
    }

    public void showBarCode(Integer id) {
        barImage = Ean18Generator.saveEanFile(id);
        barecode.setImage(barImage);
    }
    
    public void hideBarCode(){
        barecode.setImage(null);
    }
    
    public void printBarCode() {
        ImageView view = new ImageView();
        view.setImage(barImage);
        PrinterInstance.print(view);
    }
    
    
    
}
